package com.huajx.tb.server;

import com.huajx.core.handler.ServerHandlerFactory;
import com.huajx.core.server.annonation.MinaServer;
import com.huajx.core.server.impl.BaseTcpServer;
import com.huajx.core.util.MessageKit;
import com.huajx.tb.adapter.NengLongHandlerAdapter;
import com.huajx.tb.codec.NengLongDecoder;
import com.huajx.tb.codec.NengLongEncoder;
import com.huajx.tb.dto.NengLongMessage;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFactory;
import org.apache.mina.filter.codec.ProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolEncoder;
import org.apache.mina.filter.executor.ExecutorFilter;
import org.apache.mina.filter.keepalive.KeepAliveFilter;
import org.apache.mina.filter.keepalive.KeepAliveMessageFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @program: di-parent
 * @description:
 * @author: lisa 叶新华
 * @create: 2019-08-28 16:53
 */
@MinaServer(name = "nenglong", port = 10041)
public class TianboNengLongServer extends BaseTcpServer {
    private static final Logger logger = LoggerFactory.getLogger(TianboNengLongServer.class);

    public static final int DEFAULT_PORT = 10041;
    private boolean isRunning;

    private String serverName;
    private int port;

    private String describe;


    @Override
    protected ServerHandlerFactory getHandler() {
        return new NengLongHandlerAdapter();
    }

    @Override
    protected ProtocolCodecFactory getProtocolCodecFactory() {
        return new ProtocolCodecFactory() {
            @Override
            public ProtocolEncoder getEncoder(IoSession session) throws Exception {
                return new NengLongEncoder();
            }

            @Override
            public ProtocolDecoder getDecoder(IoSession session) throws Exception {
                return new NengLongDecoder();
            }
        };
    }

    @Override
    protected void initial() {
        acceptor.getSessionConfig().setReadBufferSize(1024);

        acceptor.getFilterChain().addLast("executor", new ExecutorFilter());

//        KeepAliveMessageFactory kamf = new KeepAliveMessageFactoryImpl();
//        KeepAliveFilter kaf = new KeepAliveFilter(kamf, IdleStatus.BOTH_IDLE);
//        kaf.setForwardEvent(true);
//        kaf.setRequestInterval(60); // 本服务器为被定型心跳 即需要每30秒接受一个心跳请求 否则该连接进入空闲状态
        // 并且发出idled方法回调
//        acceptor.getFilterChain().addLast("heart", kaf);

        // 读写通道60秒内无操作进入空闲状态
        acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 60000);
    }

//    private class KeepAliveMessageFactoryImpl implements KeepAliveMessageFactory {
//        private String response = null;
//
//        private boolean handle(byte[] bytes) {
//            if (bytes.length < 33) {
//                return false;
//            }
//
//            String sync = MessageKit.getStr(bytes, 0, 3);
//            String command = MessageKit.getStr(bytes, 30, 2);
//
//            return handle(sync, command, bytes);
//        }
//
//        private boolean handle(NengLongMessage message) {
//            String command = message.getCommand();
//
//            String sync = message.getSync();
//
//            return handle(sync, command, message.getBody());
//        }
//
//        private boolean handle(String sync, String command, byte[] bytes) {
//            if (sync.equals("TPJ") && "10".equals(command)) {
//                String temp = MessageKit.getStr(bytes, 3, bytes.length - 3);
//                response = "JTP" + temp;
//                if (logger.isInfoEnabled()) {
//                    logger.info("心跳检测，请求参数{}", MessageKit.getStr(bytes, 0, bytes.length));
//                }
//
//                return true;
//            }
//
//            return false;
//        }
//
//        @Override
//        public boolean isRequest(IoSession session, Object message) {
//            if (message instanceof byte[]) {
//                return handle((byte[]) message);
//            } else if(message instanceof NengLongMessage) {
//                return handle((NengLongMessage)message);
//            }
//
//            return false;
//
//        }
//
//        @Override
//        public boolean isResponse(IoSession session, Object message) {
//            if ((message instanceof String)) {
//                String msg = (String) message;
////				logger.info("包: {}", msg);
//
//                byte[] bytes = msg.getBytes();
//
//                if (bytes.length < 33) {
//                    return false;
//                }
//
//                String tbz = MessageKit.getStr(bytes, 0, 3);
//
//                String command = MessageKit.getStr(bytes, 30, 2);
//
//                if (tbz.equals("JTP") && "10".equals(command)) {
//                    if(logger.isInfoEnabled()) {
//                        logger.info("心跳检测，响应参数{}", MessageKit.getStr(bytes, 0, bytes.length));
//                    }
//                    return true;
//                }
//            }
//            return false;
//        }
//
//        @Override
//        public Object getRequest(IoSession session) {
//            return null;
//        }
//
//        @Override
//        public Object getResponse(IoSession session, Object request) {
//            return response;
//        }
//    }
}
